package com.atguigu.sparseArray;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xalan.internal.xsltc.dom.ArrayNodeListIterator;
import org.apache.commons.io.IOUtils;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Description: 稀疏数组
 * @Author: zhoufuzong
 * @Created: 2021-01-12 22:04:55
 * @Version: 1.1.0
 */

public class SparseArray {

    public static void main(String[] args) {

        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] =1;
        chessArr1[2][3] =2;
        chessArr1[4][5] =2;


//        Arrays.stream(chessArr1).forEach(x-> System.out.println(Arrays.toString(x)));

        int sum =0;
        for (int[] ints : chessArr1) {
            for (int anInt : ints) {
                if (anInt!=0){
                    sum++;
                }
            }

        }

        // 创建对应稀疏数组
        int[][] sparseArray = new int[sum+1][3];
        sparseArray[0][0] =11;
        sparseArray[0][1]=11;
        sparseArray[0][2] = sum;

        int count =0;
        for (int i = 0; i <chessArr1.length ; i++) {
            for (int j = 0; j <chessArr1[i].length; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArray[count][0] =i;
                    sparseArray[count][1] =j;
                    sparseArray[count][2]= chessArr1[i][j];
                }
            }

        }
        // 输出稀疏数组
        System.out.println();

        System.out.println("得到的稀疏数组为：如下形式~~~~");

        for (int i = 0; i < sparseArray.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n",sparseArray[i][0],sparseArray[i][1],sparseArray[i][2]);

        }

        System.out.println(sum);


        File file  = new File("sparryArray.txt");
        FileOutputStream   fos =null;
        FileInputStream fis =null;

        /*普通的for循环比较适合循环写入数据*/

        try {
            if (!file.exists()){
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            FileOutputStream finalFos = fos;

            Arrays.stream(sparseArray).peek(s-> {
                Arrays.stream(s).forEach(a->{
                    try {
                        IOUtils.write(a+"\t", finalFos,"UTF-8");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }).forEach(a->{  try {
                IOUtils.write("\r\n", finalFos,"UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }});
//            Arrays.stream(sparseArray).map(JSONObject::toJSONString).forEach(s-> {
//                try {
//                    IOUtils.write(s+"\r\n", finalFos,"UTF-8");
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            });

//
            fis = new FileInputStream("sparryArray.txt");
            List<String> list = IOUtils.readLines(fis, "UTF-8");

//            List<Integer> collect = list.stream().collect(Collectors.toList());
//            JSONArray jsonArrays = collect.get(0);
            int[] ints1 = Arrays.stream(list.get(0).split("\t")).mapToInt(Integer::parseInt).toArray();

            int[][] arr2 = new int[ints1[0]][ints1[0]];
            for (int i= 1;i<list.size() ;i ++) {
                int[] ints = Arrays.stream(list.get(i).split("\t")).mapToInt(Integer::parseInt).toArray();
                arr2[ints[0]][ints[1]]=ints[2];
            }
//
            for (int[] ints : arr2) {
                for (int anInt : ints) {
                    System.out.printf("%s\t",anInt);

                }
                System.out.println();

            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(fos);
            IOUtils.closeQuietly(fis);
        }




    }

    private static void listMethod() {
        // 创建一个原始的二维数组 11*11
        // 0：表示没有棋子，1 表示黑子，2 表示 男子

        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] =1;
        chessArr1[2][3] =2;
        chessArr1[4][5] =2;
        //输出原始的数组
        for (int[] ints : chessArr1) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }


        // 将二维数组 转稀疏数组的处理
        //1.先遍历二维数组，得到非0数据的个数
        int sum =0;
        for (int[] ints : chessArr1) {
            for (int anInt : ints) {
                if (anInt != 0) {
                    sum++;
                }

            }
        }

        //2. 创建对应的稀疏数组
        int sparseArray[][] = new int[sum+1][3];

        // 给稀疏数组赋值
        sparseArray[0][0] =11;
        sparseArray[0][1]=11;
        sparseArray[0][2]=sum;

        // 遍历二维数组，将非零的值，存入稀疏数组中
        int count =0; // 用于记录是第几个非零数据
        for (int i = 0; i <11 ; i++) {
            for (int j = 0; j <11 ; j++) {
                if (chessArr1[i][j] !=0){
                    count++;
                    sparseArray[count][0]=i;
                    sparseArray[count][1]=j;
                    sparseArray[count][2]=chessArr1[i][j];
                }

            }

        }
        // 输出稀疏数组
        System.out.println();

        System.out.println("得到的稀疏数组为：如下形式~~~~");

        for (int i = 0; i < sparseArray.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n",sparseArray[i][0],sparseArray[i][1],sparseArray[i][2]);

        }

        // 将稀疏数组恢复成原始的二维数组
        /*
        1.从本地读取文件
        2.先读取稀疏数组的第一行，根据第一行的数据，创建原始的二维数组，比如上面的 chessArr2 =int[11][11]
        3.在读取稀疏数组后几行的数据，并赋值给原始的二维数组即可
         */
        int chessArr2[][] =new int[sparseArray[0][0]][sparseArray[0][1]];
        for (int i = 1; i <sparseArray.length ; i++) {
            chessArr2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }

        System.out.println("原始数组~~~");
        for (int[] ints : chessArr2) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();

        }
    }
}
